package cn.iocoder.yudao.module.acm.service.servicesupplydistplan;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.acm.controller.admin.servicesupplydistplan.vo.ServiceSupplyDistPlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.servicesupplydistplan.vo.ServiceSupplyDistPlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.servicesupplydistplan.vo.ServiceSupplyDistPlanSaveReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.onboardsupply.OnboardSupplyDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.servicesupplydistplan.ServiceSupplyDistPlanDO;
import cn.iocoder.yudao.module.acm.dal.mysql.onboardsupply.OnboardSupplyMapper;
import cn.iocoder.yudao.module.acm.dal.mysql.servicesupplydistplan.ServiceSupplyDistPlanMapper;
import cn.iocoder.yudao.module.acm.enums.ACMTablewareDistStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.acm.enums.ErrorCodeConstants.TABLEWARE_DIST_PLAN_NOT_EXISTS;

/**
 * 机供品餐具配送计划 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class ServiceSupplyDistPlanServiceImpl implements ServiceSupplyDistPlanService {

    @Resource
    private ServiceSupplyDistPlanMapper serviceSupplyDistPlanMapper;
    @Resource
    private OnboardSupplyMapper onboardSupplyMapper;


    @Override
    public Long createTablewareDistPlan(ServiceSupplyDistPlanSaveReqVO createReqVO) {
        // 插入
        ServiceSupplyDistPlanDO tablewareDistPlan = BeanUtils.toBean(createReqVO, ServiceSupplyDistPlanDO.class);
        tablewareDistPlan.setStartDate(createReqVO.getExpireDate()[0]);
        tablewareDistPlan.setEndDate(createReqVO.getExpireDate()[1]);
        tablewareDistPlan.setPlanCode("D"+System.currentTimeMillis());
        if (ObjectUtil.isEmpty(tablewareDistPlan.getDistStatus())) {
            tablewareDistPlan.setDistStatus(Integer.valueOf(ACMTablewareDistStatusEnum.TABLEWARE_DIST_STATUS_1.getCode()));
        }
        serviceSupplyDistPlanMapper.insert(tablewareDistPlan);
        // 返回
        return tablewareDistPlan.getId();
    }

    @Override
    public void updateTablewareDistPlan(ServiceSupplyDistPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateTablewareDistPlanExists(updateReqVO.getId());
        // 更新
        ServiceSupplyDistPlanDO updateObj = BeanUtils.toBean(updateReqVO, ServiceSupplyDistPlanDO.class);
        serviceSupplyDistPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteTablewareDistPlan(Long id) {
        // 校验存在
        validateTablewareDistPlanExists(id);
        // 删除
        serviceSupplyDistPlanMapper.deleteById(id);
    }

    @Override
        public void deleteTablewareDistPlanListByIds(List<Long> ids) {
        // 校验存在
        validateTablewareDistPlanExists(ids);
        // 删除
        serviceSupplyDistPlanMapper.deleteByIds(ids);
        }

    private void validateTablewareDistPlanExists(List<Long> ids) {
        List<ServiceSupplyDistPlanDO> list = serviceSupplyDistPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TABLEWARE_DIST_PLAN_NOT_EXISTS);
        }
    }

    private void validateTablewareDistPlanExists(Long id) {
        if (serviceSupplyDistPlanMapper.selectById(id) == null) {
            throw exception(TABLEWARE_DIST_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public ServiceSupplyDistPlanDO getTablewareDistPlan(Long id) {
        return serviceSupplyDistPlanMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceSupplyDistPlanRespVO> getTablewareDistPlanPage(ServiceSupplyDistPlanPageReqVO pageReqVO) {
        if (ObjectUtil.isNotEmpty(pageReqVO.getExpireDate())) {
            pageReqVO.setStartDate(pageReqVO.getExpireDate()[0]);
            pageReqVO.setEndDate(pageReqVO.getExpireDate()[1]);
        }
        PageResult<ServiceSupplyDistPlanDO> result = serviceSupplyDistPlanMapper.selectPage(pageReqVO);
        return new PageResult<>(buildTablewareDistPlanDetailList(result.getList()), result.getTotal());
    }

    private List<ServiceSupplyDistPlanRespVO> buildTablewareDistPlanDetailList(List<ServiceSupplyDistPlanDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1 获取餐具列表
        List<OnboardSupplyDO> supplyDOS = onboardSupplyMapper.selectByIds(convertList(list, ServiceSupplyDistPlanDO::getSupplyId));
        Map<Long, OnboardSupplyDO> convertMap = CollectionUtils.convertMap(supplyDOS, OnboardSupplyDO::getId);
        // 2. 转换成 VO
        return BeanUtils.toBean(list, ServiceSupplyDistPlanRespVO.class, respVO ->
                MapUtils.findAndThen(convertMap, respVO.getSupplyId(), supply -> respVO.setSupplyName(supply.getName())));
    }

}